package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;

import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.model.TbSpecification;
import com.dongdong.model.TbSpecificationExample;
import com.dongdong.model.TbSpecificationOption;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.result.Result;
import com.dongdong.result.ResultConstant;
import com.dongdong.service.BrandService;
import com.dongdong.service.SpecService;
import com.dongdong.service.SpecificationOptionService;
import com.dongdong.vo.SpecificationOptionVO;
import com.dongdong.vo.SpecificationVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private SpecificationOptionService specificationOptionService;


    @Override
    public List<SpecificationVO> findAll() {
        List<TbSpecification> tbSpecificationList = tbSpecificationMapper.selectByExample(null);
        // 把brandList 中的泛型TbBrand转换为SpecificationVO
/*        ArrayList<SpecificationVO> vos = new ArrayList<>();
        for (TbBrand tbBrand : tbSpecificationList){
            SpecificationVO SpecificationVO = new SpecificationVO();
            BeanUtils.copyProperties(tbBrand, SpecificationVO);
            vos.add (SpecificationVO);
        }*/
        //使用jdk8新特性[1,2,3,4,5,6]
        List<SpecificationVO> vos = tbSpecificationList.stream().map((x) -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());//将流转换成list集合
        return vos;
    }

   //查询全部+分页+模糊搜索
    @Override
    public PageResult<SpecificationVO> findAllPage(PageBean<SpecificationVO> pageBean) {

        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample. Criteria criteria = example.createCriteria();
      if(!StrUtil.isEmpty(pageBean.getBean().getSpecName())){
            criteria. andSpecNameLike("%"+pageBean.getBean().getSpecName()+"%");
        }
        //分页部分
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<TbSpecification> tbSpecificationList = tbSpecificationMapper.selectByExample(example);
        PageInfo<TbSpecification> info = new PageInfo<>(tbSpecificationList);
        //转换
         List<SpecificationVO>  vos = info.getList().stream().map((x) ->  {
         SpecificationVO vo = new SpecificationVO();
         BeanUtils. copyProperties(x, vo);
         return vo;
        }). collect (Collectors.toList());

         return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), vos);
    }

    //增加
    @Override
    @Transactional
    public void insert(SpecificationVO specVO) {
        TbSpecification specification = new TbSpecification();
        BeanUtils.copyProperties(specVO,specification);
         tbSpecificationMapper.insertbyid(specification);//增加成功后返回主键id
        Long id = specification.getId();
/*        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample. Criteria criteria = example.createCriteria();
        criteria.andSpecNameEqualTo(specVO.getSpecName());
        List<TbSpecification> tbSpecificationList = tbSpecificationMapper.selectByExample(example);*/


        //增加对应数据
        List<TbSpecificationOption> collect = specVO.getOptionVOS().stream().map((x) -> {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(x, option);
            return option;
        }).collect(Collectors.toList());
        specificationOptionService.addlist(collect,specification.getId());

    }
//回显
    @Override
    public SpecificationVO selectbyid(Long id) {

        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        //转换
        SpecificationVO SpecificationVO = new SpecificationVO();
        BeanUtils.copyProperties(tbSpecification,SpecificationVO);

        //根据specid查询组成信息
        List<SpecificationOptionVO> optionVOS = specificationOptionService.findSpecificationOptionList(id);
        SpecificationVO.setOptionVOS(optionVOS);

        return SpecificationVO;
    }
//修改
    @Override
    @Transactional //事务
    public void update(SpecificationVO specVO) {
        Long id = specVO.getId();
//         tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //1. 更新规格表
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils. copyProperties(specVO, tbSpecification);
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        // 2.根据 spec_id 删除相关数据
//        specificationOptionService.deletebySpecId(specVO.getId());
        List<SpecificationOptionVO> optionVOS = specVO.getOptionVOS();
        for (SpecificationOptionVO optionVO : optionVOS) {
            specificationOptionService.deletebyId(optionVO.getId());
        }

        // 3,把specV0中的List添加到规则组成表中
        List<SpecificationOptionVO> optionVOSList = specVO.getOptionVOS();
        for (SpecificationOptionVO optionVO : optionVOSList) {
            TbSpecificationOption tbSpecificationOption = new TbSpecificationOption();
            BeanUtils. copyProperties(optionVO, tbSpecificationOption);
            tbSpecificationOption.setSpecId(id);
            specificationOptionService.add(tbSpecificationOption);
        }

    }
//删除
    @Override
    @Transactional
    public void deletebyid(Long id) {
        tbSpecificationMapper.deleteByPrimaryKey(id);

        specificationOptionService.deletebySpecId(id);

    }

//批删
    @Override
    @Transactional
    public int deletebyids(Integer[] ids) {
        TbSpecificationExample tre = new TbSpecificationExample();

        List<Integer> list = Arrays.asList(ids);
        List<Long> list2 = new ArrayList<>();

        for (Integer i : list) {
            int i1 = i.intValue();
            list2.add(Long.valueOf(i1));
        }

        tre.createCriteria().andIdIn(list2);
        int i = tbSpecificationMapper.deleteByExample(tre);

        specificationOptionService.deleteAll(list2);
       return i;

    }


}
